import axios from "axios";

// 获取 access_token
export const getAccessToken = async (appId: string, secret: string, code: string) => {
  try {
    const response = await axios.get(
      `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appId}&secret=${secret}&code=${code}&grant_type=authorization_code`
    );
    return response.data;
  } catch (error) {
    console.error("获取 access_token 失败", error);
    return null;
  }
};

// 获取用户信息
export const getUserInfo = async (accessToken: string, openid: string) => {
  try {
    const response = await axios.get(
      `https://api.weixin.qq.com/sns/userinfo?access_token=${accessToken}&openid=${openid}&lang=zh_CN`
    );
    return response.data;
  } catch (error) {
    console.error("获取用户信息失败", error);
    return null;
  }
};